home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Magazin/MacEasy 24
/
Mac Magazin and MacEasy Magazine CD - Issue 24.iso
/
Wissenschaft & Technik
/
Sys Environment (PPC) Dev ƒ
/
Source
/
sysEnv.cpp
< prev
Wrap
C/C++ Source or Header
|
1996-08-12
|
20KB
|
670 lines
//sysEnv.c ©1996 Brian Bergstrand 07/16/96
#include <stdio.h>
#include "sysEnv.h"
#include "machines.h"
#include "systems.h"
//globals
AppGlobals *global = new AppGlobals;//create an instance and allocate memory
//main function - returns nothing
void main (void)
{
int whichCursor;
whichCursor = kWatchCursor;
SwitchCursor (&whichCursor);//sets the cursor to the watch
InitToolBox();//Intialize the ToolBox
//test for gestalt, don't really need to do this, but Apple still recommends it
long response;
OSErr myErr = Gestalt (gestaltVersion, &response);
if (myErr)//if gestalt is not present
HandleError (2, true, 0L);//call our error handling code
MenuBarInit();
SetCursor (&qd.arrow);//sets the cursor back to the arrow
HandleEnvDialog();//show the dialog
EventLoop();//then start the main event loop
}
//SwitchCursor function - returns nothing
void SwitchCursor (int *whichCursor)
{
Cursor myCursor;
CursHandle cursorH;
if (*whichCursor==kWatchCursor)//if the cursor choice = watch
cursorH = GetCursor (watchCursor);//get a handle to the watch cursor
HLock ((Handle) cursorH);//lock the handle
myCursor = **cursorH;
HUnlock ((Handle) cursorH);//unlock the handle
SetCursor (&myCursor);//set the cursor
}
//ToolBoxInit function - returns nothing
void InitToolBox (void)
{//intialize all of the TB managers
InitGraf (&qd.thePort);
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs(0L);
InitCursor();
MaxApplZone();//we can use this now, but Mac OS 8 will break with this call
FlushEvents(everyEvent, 0);//flush all cued events
MoreMasters();//we can use this now, but Mac OS 8 will break with this call
}
//MenuBarInit function - returns nothing
void MenuBarInit (void)
{
Handle menuBar;
MenuHandle menu;
menuBar = GetNewMBar (kBaseResID);//get a new menu bar
SetMenuBar (menuBar);//set the menu bar
menu = GetMHandle (mApple);//add the Apple Menu
AddResMenu (menu, 'DRVR');//add all the Apple Menu Items
DrawMenuBar ();//draw the menu bar
}
//EventLoop function - returns nothing
void EventLoop (void)
{
EventRecord myEvent;//data struct to hold the Event Record returned by WNE
do//start our main polling loop
{
if (WaitNextEvent (everyEvent, &myEvent, kSleep, 0L) )//poll for events
{
if (IsDialogEvent(&myEvent) == true)//if the event belongs to the dialog
{
HandleDialogEvent(&myEvent);//pass it to let the dialog handle it
continue;
}
else//else let the main app handle it
DoEvent (&myEvent);
}
}while(!global->done) ;//keep looping until the user quits
Quit();//when were done call the cleanup function
}
//DoEvent function -returns nothing
void DoEvent (EventRecord *eventPtr)
{
char theChar;
if (eventPtr->what==mouseDown)//check for a mouseDown event
HandleMouseDown (eventPtr);
else
switch (eventPtr->what)//in this program the switch() is overkill,
{ //but if more events need to be handled,
case keyDown: //way to do it.
case autoKey:
theChar=eventPtr->message & charCodeMask;//set the char bit
//checks to see if a command key modifier was pressed
if ( (eventPtr->modifiers &cmdKey) != 0)
{
long modifier = MenuKey (theChar);
HandleMenuChoice (&modifier);//menu command keys
}
break;
}
}
//HandleMouseDown function - returns nothing
void HandleMouseDown (EventRecord *eventPtr)
{
WindowPtr whichWindow;
MenuHandle fileMenu;
short thePart;
long menuChoice;
thePart = FindWindow (eventPtr->where, &whichWindow);//what window did the user click
if (thePart==inMenuBar)//if the user clicked in the menu bar
{
fileMenu = GetMHandle (mFile);//get a handle to the file menu
if (global->dialogActive)//if there is already a dialog open
DisableItem (fileMenu, iGetInfo);//disable the menu item
else
EnableItem (fileMenu, iGetInfo);//enable the item
menuChoice=MenuSelect (eventPtr->where);//what menu did the user choose?
HandleMenuChoice (&menuChoice);//handle the user's choice
}
else
switch (thePart)
{
case inSysWindow://did the user click in the Finder?
SystemClick (eventPtr, whichWindow);//let the System handle it
break;
case inDrag://let the user drag the window around the whole screen
DragWindow (whichWindow, eventPtr->where, &qd.screenBits.bounds);
break;
}
}
//HandleMenuChoice function - returns nothing
void HandleMenuChoice (long *menuChoice)
{
short menu;
short item;
if (*menuChoice != 0)
{
menu=HiWord (*menuChoice);//the menu
item= LoWord (*menuChoice);//the menu item
switch (menu)
{
case mApple:
HandleAppleChoice (&item);
break;
case mFile:
HandleFileChoice (&item);
break;
}
HiliteMenu (0);
}
}
//HandleAppleChoice function - returns nothing
void HandleAppleChoice (short *item)
{
MenuHandle appleMenu;
Str255 accName;
short accNumber;
switch (*item)
{
case iAbout://this handles the whole About dialog
DialogPtr dialog;//handle to the dialog box
Handle textItemHandle;
Rect itemRect;
short itemHit, itemType, text=1;
Boolean dialogDone=false;
dialog = GetNewDialog (kDialogResID, 0L, kMoveToFront);//load the dialog
SetPort (dialog);//set the current port to the dialog
GetDItem (dialog, text, &itemType, &textItemHandle, &itemRect);//get the only item
ShowWindow (dialog);//show the dialog
while (!dialogDone)
{
ModalDialog (0L, &itemHit);//retrieve user item hits
if (itemHit==text)//when the user clicks in the dialog box, exit
dialogDone=true;//exit loop
}
DisposeDialog (dialog);//free memory
break;
default://the user chose a DA
appleMenu=GetMHandle (mApple);
GetItem (appleMenu, *item, accName);//which App
accNumber=OpenDeskAcc (accName);
break;
}
}
//HandleFileChoice function - returns nothing
void HandleFileChoice (short *item)
{
switch (*item)
{
case iGetInfo:
HandleEnvDialog();//show the dialog box
break;
case iQuit:
global->done=true;//exit EventLoop
break;
}
}
//HandleEnvDialog - returns nothing
void HandleEnvDialog (void)
{
DialogPtr dialog;//ptr to the dialog box
Handle okItemH;
Rect itemRect;//Ok item rect
TextHandles *text = new TextHandles;//declare an instance and allocate memory
short itemType;
dialog = GetNewDialog (kDialogResID+1, 0L, kMoveToFront);//load the dialog
SetPort (dialog);//set the port
GetDItem (dialog, ok, &itemType, &okItemH, &itemRect);//get the button
//Get all of the text boxes we need to change
GetDItem (dialog, iSysVer, &itemType, &text->sysVerH, &itemRect);
GetDItem (dialog, iUpdateVer, &itemType, &text->updateVerH, &itemRect);
GetDItem (dialog, iMachine, &itemType, &text->machineH, &itemRect);
GetDItem (dialog, iProcessor, &itemType, &text->processorH, &itemRect);
GetDItem (dialog, iRealMem, &itemType, &text->realMemH, &itemRect);
GetDItem (dialog, iLogicalMem, &itemType, &text->logicalMemH, &itemRect);
GetDItem (dialog, iMachineID, &itemType, &text->machineIDH, &itemRect);
GetSysEnv(&text);//get system environment
ShowWindow (dialog);//show the dialog
global->dialogActive = true;//set the flag to de-activate the menu item
}
//GetSysEnv - returns nothing
void GetSysEnv (TextHandles **text)
{ //allocate memory for this class instance
MachineEnv *thisMachine = new MachineEnv;
char cTempString[256];//temporary storage for C strings
Str255 *pTempString=NULL;//temporary storage for Pascal strings
//get all of the system variables
long mySystem = thisMachine->ReturnSysVer();//get the system version
pTempString = FindSystem (&mySystem);
SetStaticString ((*text)->sysVerH, pTempString);
long myUpdate = thisMachine->ReturnUpdateVer();//get the update version
if (mySystem>=_753)//if the system is greater than 7.5.2
{
if (myUpdate==_20)//check if it is an update
GetIndString (*pTempString, kBaseResID+3, 2);//get the udpate string
else
GetIndString (*pTempString, kBaseResID+3, 3);//get the non-udpate string
}
else
GetIndString (*pTempString, kBaseResID+3, 1);//get a error string
SetStaticString ((*text)->updateVerH, pTempString);//set the string in the text box
long myMachine = thisMachine->ReturnMachineType();//get the machine ID
/*sprintf() is used because the overhead of streams can not be justified for a program
of this size*/
sprintf (cTempString, "%ld", myMachine);//buffer the ID number
SetStaticString ((*text)->machineIDH, cTempString);
pTempString = FindMachine (&myMachine, &thisMachine);//get the string to the machine
SetStaticString ((*text)->machineH, pTempString);
long myProcessor = thisMachine->ReturnProcessor();//get the processor
pTempString = FindProcessor (&myProcessor);
SetStaticString ((*text)->processorH, pTempString);
sprintf (cTempString, "%ld Kb", (thisMachine->ReturnRealMem()/1024));//buffer the string
SetStaticString ((*text)->realMemH, cTempString);
sprintf (cTempString, "%ld Kb", (thisMachine->ReturnLogicalMem()/1024));//buffer the string
SetStaticString ((*text)->logicalMemH, cTempString);
delete thisMachine;//free memory
}
//FindMachine function - returns a pointer to a String resource
Str255 * FindMachine (long *machineID, MachineEnv **thisMachine)
{
Str255 *tempString=NULL;//holds the string if found
short whichMachine=0;//used to get the string representing the machine
Boolean performa=false;
switch (*machineID)
{
case gestaltPowerMac6100_60://power macs
whichMachine=i6100_60;//set the correct string number
break;
case gestaltPowerMac7100_66:
whichMachine=i7100_66;
break;
case gestaltPowerMac8100_80:
whichMachine=i8100_80;
break;
case gestaltPowerMac6100_66:
whichMachine=i6100_66;
break;
case gestaltPowerMac7100_80:
whichMachine=i7100_80;
break;
case gestaltPowerMac8100_100:
whichMachine=i8100_100;
break;
case gestaltPowerMac8100_110:
whichMachine=i8100_110;
break;
case gestaltPowerMac7200:
whichMachine=i7200;
break;
case gestaltPowerMac7500:
if (((*thisMachine)->ReturnProcessor())==(long)gestaltCPU601)//if the CPU is a 601
whichMachine=i7500;//the machine is a 7500
else
whichMachine=i7600;//the machine is a 7600
break;
case gestaltPowerMac8500:
whichMachine=i8500;
break;
case gestaltPowerMac9500:
whichMachine=i9500;
break;
case gestaltAWS9150_80://workgroup servers
whichMachine=iAWS9150_80;
break;
case gestaltAWS9150_120:
whichMachine=iAWS9150_120;
break;
case gestaltPowerBookDuo2300://powerbooks
whichMachine=i2300;
break;
case gestaltPowerBook500PPCUpgrade:
whichMachine=i500upgrade;
break;
case gestaltPowerBook5300:
whichMachine=i5300;
break;
case gestaltPowerMac5200://performas
whichMachine=i5200;
performa=true;
break;
/*case gestaltPowerMac5215: I do not know the ID's of these machines,
whichMachine=i5215; if you have one please send me the number.
performa=true;
break;
case gestaltPowerMac6110:
whichMachine=i6110;
performa=true;
break;
case gestaltPowerMac6112:
whichMachine=i6112;
performa=true;
break;
case gestaltPowerMac6115:
whichMachine=i6115;
performa=true;
break;
case gestaltPowerMac6116:
whichMachine=i6116;
performa=true;
break;
case gestaltPowerMac6117:
whichMachine=i6117;
performa=true;
break;
case gestaltPowerMac6118:
whichMachine=i6118;
performa=true;
break;*/
case gestaltPowerMac6200:
whichMachine=i6200;
performa=true;
break;
/*case gestaltPowerMac6300: Same as above for this machine class,
whichMachine=i6300; I need the ID number.
performa=true;
break;*/
default://machine is not on file
whichMachine=iNotFound;
break;
}
if (!performa)//if the machine is not a peforma look in the PwrMac list
GetIndString (*tempString, kBaseResID, whichMachine);//the machine
else//look in the Performa list
GetIndString (*tempString, kBaseResID+1, whichMachine);//the performa machine
return tempString;
}
//FindSystem function - returns a pointer to a String resource
Str255 * FindSystem (long *systemVer)
{
Str255 *tempString=NULL;//holds the string if found
short whichSystem=0;//used to get the string representing the machine
switch (*systemVer)
{
case _712:// v7.1.2
whichSystem=i712;
break;
case _750:// v7.5
whichSystem=i750;
break;
case _751:// v7.5.1
whichSystem=i751;
break;
case _752:// v7.5.2
whichSystem=i752;
break;
case _753:// v7.5.3
whichSystem=i753;
break;
default://system is not on file
whichSystem=iSysNotFound;
break;
}
GetIndString (*tempString, kBaseResID+2, whichSystem);//get the system string
return tempString;
}
//FindProcessor function - returns a pointer to a String resource
Str255 * FindProcessor (long *processor)
{
Str255 *tempString=NULL;//holds the string if found
short whichProcessor=0;//used to get the string representing the machine
switch (*processor)
{
case gestaltCPU601:// 601
whichProcessor=i601;
break;
case gestaltCPU603:// 603
whichProcessor=i603;
break;
case gestaltCPU604:// 604
whichProcessor=i604;
break;
case gestaltCPU603e://603e - defined in machines.h
whichProcessor=i603e;
break;
case gestaltCPU604e://604e - defined in machines.h
whichProcessor=i604e;
break;
default:
whichProcessor=iCPUNotFound;
break;
}
GetIndString (*tempString, kBaseResID+4, whichProcessor);//get the udpate string
return tempString;
}
//SetStaticString function (Pascal style string) - returns nothing
void SetStaticString (Handle whichHandle, Str255 *tempString)
{
SetHandleSize (whichHandle, (Size)*tempString[0]+1);//size the handle
HLock (whichHandle);//lock the handle
//copy the string to the field
SetIText (whichHandle, *tempString);
HUnlock (whichHandle);//unlock the handle
}
//SetStaticString function (C style string) - reuturns nothing
void SetStaticString (Handle whichHandle, char *tempString)
{
CtoPstr (tempString);//translate to a pascal string
SetHandleSize (whichHandle, (Size)tempString[0]+1);//size the handle
HLock (whichHandle);//lock the handle
//copy the string to the field
SetIText (whichHandle, (const unsigned char *)tempString);
HUnlock (whichHandle);//unlock the handle
}
//HandleDialogEvent function - returns nothing
void HandleDialogEvent(EventRecord *eventPtr)
{
DialogPtr dialog = (DialogPtr)FrontWindow();
GrafPtr origPort;
short myItem;
GetPort(&origPort);//store the original port
SetPort(dialog);//then set the port to the dialog
if (EventFilter(eventPtr, dialog) != false)//run through our filter
{
if (DialogSelect(eventPtr, &dialog, &myItem) == true)//if our button was clicked
{
if (dialog != 0L)//if the dialog is not NULL
{
switch (myItem)
{
case ok://if ok was hit doing the switch, just dispose of the dialog
//Internal error handiling, not needed in the Final
//HandleError(0, false, "\pDestroying Dialog");
DisposeDialog (dialog);//free memory
global->dialogActive = false;//then set the flag to show the menu item
break;
}
}
}
}
SetPort (origPort);//restore the original port
}
//EventFilter function - returns a Boolean value
Boolean EventFilter(EventRecord *myEvent, WindowPtr myFrontWindow)
{
short ok=1;
switch (myEvent->what)
{
case activateEvt://if it is an activate event
//Take care of hiliting the
//Ok button according to whether or not the dialog is in front.
myFrontWindow = (WindowPtr)myEvent->message;
ControlHandle okHandle = GetCtlHandle((DialogPtr)myFrontWindow, ok);
if ((myEvent->modifiers & activeFlag) == true)
HiliteControl(okHandle, 0);//if we are active
else
HiliteControl(okHandle, 255);//if we are in the background
return false;
break;
}
return true;//if no events were processed return true
}
//GetCtlHandle function - returns a Handle to a control item
static ControlHandle GetCtlHandle(DialogPtr dialog, short item)
{
Handle myControl;
short itemType;
Rect itemRect;
GetDItem(dialog, item, &itemType, &myControl, &itemRect);//get the handle
if (myControl == 0L)//if the handle is null
HandleError(3, true, 0L);//call our error handling code
return (ControlHandle)myControl;//return the handle
}
//HandleError function - returns nothing
void HandleError (short stringID, Boolean fatal, Str255 debugStr)
{
Str255 errStr;
Str15 fatalStr;
short itemHit;
//set the cursor back to the arrow, could check for what the cursor is,
//but this won't hurt
SetCursor (&qd.arrow);
if (fatal && !debugStr)//if the error is fatal, and not a debug string
{
GetIndString (fatalStr, kErrorStringsID, 1);//get the notification string
GetIndString (errStr, kErrorStringsID, stringID);//get the error string
ParamText (fatalStr, errStr, "\p ", "\p ");//set the paramter strings
}
else
if (!fatal && !debugStr)//if the error is not fatal, and not a debug string
{
GetIndString (errStr, kErrorStringsID, stringID);//get the error string
ParamText (errStr, "\p ", "\p ", "\p ");//set the paramter strings
}
else
if (debugStr)//if it is just a debug string
ParamText (debugStr, "\p ", "\p ", "\p ");//set the paramter strings
if (fatal)//if this is a fatal error
{
itemHit = StopAlert (kAlertID, 0L);//alert user
Quit();//then exit right away
}
else//warn the user
itemHit = CautionAlert (kAlertID, 0L);//alert user
}
//Quit function - returns nothing
inline void Quit (void)
{
//Internal error handiling, not needed in the Final
//HandleError(0, false, "\pDeleting global struct 'AppGlobals'.");
delete global;//free the global memory before we quit
ExitToShell();//then exit
}